// hospital/pages/registrationHistory/registrationHistory.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    registerList: []
  },

  // 取消
  cancelHandle(e) {
    var index = e.currentTarget.dataset.index,
      that = this;
    wx.showModal({
      title: '您好',
      content: '是否要取消该预约',
      complete: (res) => {
        if (res.confirm) {
          console.log(index);
          that.cancelRequest(index)
        }
      }
    })
  },

  cancelRequest(index) {
    console.log(index);
    var list = this.data.registerList;
    console.log(list[index]);

    var url = app.globalData.url + "doctor/cancelUserRegister",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        registerId: list[index].registerId,
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          wx.showModal({
            title: '您好',
            content: '您已成功取消预约',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                that.getList()
              }
            }
          })
        }
      },
      err => {
        console.log(err);
      })
  },


  getList() {
    var url = app.globalData.url + "doctor/listUserRegister",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        userId: app.globalData.userId,
      }, res => {
        console.log(res);
        if (res.flag) {
          wx.hideLoading()
          var datas = res.data;
          console.log(datas);
          datas.forEach(item => {
            item.imgUrl = `/hospital/img/${item.departmentId||6}.png`
            item.time = item.registerDate + ' ' + item.timeSlot.split('-')[0]
            console.log(new Date(this.getNow()).getTime());
            console.log(new Date(item.time).getTime());
            item.isCancel = new Date(item.time).getTime() > new Date(this.getNow()).getTime()

          })
          console.log(datas);
          that.setData({
            registerList: datas
          })

        }
      },
      err => {
        console.log(err);
      })
  },
  getNow: function () {
    var date = new Date()

    var year = date.getFullYear()
    var month = this.add0(date.getMonth() + 1)
    var day = this.add0(date.getDate())
    var hours = this.add0(date.getHours())
    var str = year + '-' + month + '-' + day + ' ' + hours + ':00'
    return str
  },
  add0: function (m) {
    return m < 10 ? '0' + m : m
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getList()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})